<?php defined('SYSPATH') or die('No direct script access.');
/**
 * @author thanh.dang
 * @todo Land Controller
 */

class Controller_Admin_Land extends Controller_Admin_Layout
{
	function action_manage()
	{
		if(Request::current()->method() === 'POST'){
			
			$valid = Validation::factory($_POST)
									->rule('title','not_empty')
									->rule('title','max_length',array('title',255))
									->rule('contact_name','not_empty')
									->rule('contact_phone','not_empty');
									
			if($valid->check()){
				if(isset($_POST['id'])){
					//update land
                    $saved = Libs_Land::getInstance()->saveLand($_POST,false);
                    
				}else{
					//create new land
                    $saved = Libs_Land::getInstance()->saveLand($_POST);
				}

				if(!$saved)
					FlashMessage::getInstance()->add("An error occured. Please try again");
			}else{
				FlashMessage::getInstance()->add($valid->errors('land',true));
			}
			
		}
		
		$this->add_scripts(Url::vendors().'tiny_mce/jquery.tinymce.js');
		$this->add_scripts(Url::admin_assets().'js/tinymce.js');
		
		$this->set_content(View::admin('land/manage')->set(array(
			'list' => Libs_Land::getInstance()->get_list('admin/land/list',array('status' => Libs_Land::STATUS_APPROVAL)),
			'form' => View::admin('land/form/land')->set(array(
				'land' => new Land(),
			)),
			'location' => Libs_Land::getInstance()->get_location()
		)));
	}
	
    function action_pending()
    {
        $this->set_content(View::admin('land/manage')->set(array(
			'list' => Libs_Land::getInstance()->get_list('admin/land/list',array('status' => Libs_Land::STATUS_PENDING)),
			'location' => Libs_Land::getInstance()->get_location()
		)));
    }
    
	function action_edit($id = null)
	{
		if(is_null($id)){
			$this->response->status(404);
		}else{
			
			$land = LandTable::getInstance()->findOneById($id);
			if(!$land)
				FlashMessage::getInstance()->add('Land not found !');
				
			$this->add_scripts(Url::vendors().'tiny_mce/jquery.tinymce.js');
			$this->add_scripts(Url::admin_assets().'js/tinymce.js');	
			$this->set_content(View::admin('land/edit')->set(array(
				'form' => View::admin('land/form/land')->set(array(
						'land' => !$land ? new Land() : $land,
						'location' => Libs_Land::getInstance()->get_location()
					))
			)));
		}
	}
	
	function action_delete($id)
	{
		if(is_numeric($id)){
			$row = Doctrine_Query::create()
								->delete('Land l')
								->where('l.id = ?',$id)->execute();
			if($row == 1){
				FlashMessage::getInstance()->add("Xóa dự án thành công.",FlashMessage::SUCCESS, true);
			}else{
				FlashMessage::getInstance()->add("Đã có lỗi xảy ra. Hãy thử lại lần nữa.",FlashMessage::ATTENTION, true);
			}
			$this->request->redirect(Route::url('admin',array('controller' => 'land','action' => 'manage')));
		}
	}
	
	function action_search()
	{
		$params = $this->request->query();
		$this->set_content(View::admin('land/manage')->set(array(
			'list' => Libs_Land::getInstance()->get_list('admin/land/list', $params),
			'form' => View::admin('land/form/land')->set(array(
				'land' => new Land(),
			)),
			'location' => Libs_Land::getInstance()->get_location()
		)));
	}
}